package com.situ.controller;

import com.situ.service.ProductService;
import com.situ.service.UserService;
import com.situ.service.impl.ProductServiceImpl;
import com.situ.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yangkx
 * @create 2023-08-28-16:23
 */
@WebServlet("/login")
public class LoginController extends HttpServlet {

    //从浏览器直接打开登陆页面
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/login.jsp").forward(req,resp);
    }

    //点击登录按钮进行判断
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //校验用户名，密码，是否正确
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //派UserService校验
        UserService service = new UserServiceImpl();
        boolean b = service.check(username,password);
        if (b){
            //一旦登陆成功，就在session中存一个值（打上个标记）
            //定位是在哪个会话session，一个浏览器连接一个会话
            req.getSession().setAttribute("currentUser",username);//( "标记名" ,标记值 )

            //如果true 重定向到管理系统主页
            resp.sendRedirect("/index");
        }else {
            //如果false 请求转发，带数据回到登陆页面
            req.setAttribute("errorMsg","用户名或密码错误");
            req.getRequestDispatcher("WEB-INF/login.jsp").forward(req,resp);
        }

        //四大作用域 由小到大
        //1.page页面 2.request请求 3.session会话 4.application应用程序
        //EL表达式获取 从小到大挨个查找
    }
}
